<?php
/**
 * 角色
 * Enter description here ...
 * @author Administrator
 *
 */
namespace System\Controller;
use Common\Controller\BaseController;
class SysRoleController extends BaseController{
	const modelName=CONTROLLER_NAME;
	
	protected function _initialize(){
		parent::_initialize();

		$this->checkAuth();

		$this->model = D(self::modelName);
	}
	
	public function index(){
        if(IS_AJAX){
            //获取列表
            $field = 'pk_id,name,description,create_time';
            $this->data = $this->getRows(self::modelName, '' ,$field ,1);
            $this->rtnResult(0);
        }else{
            //表头、字段名
            $data['field'] = 'pk_id,name,description,create_time';
            $data['titleArr'] = array('编号','角色名','描述','添加时间');
            $this->showView($data,'./Public/Admin/Tpl/index.html');
        }
	}

    /**
     * 编辑数据
     */
    public function edit(){
    	if (IS_POST){
    		//更新操作
    		$this->editData($this->model,'修改成功|修改失败|ajax','index');
    	}else {
    		//页面视图
    		$conditions['pk_id'] = I('get.pk_id');
    		$field = 'pk_id,name,description,create_time';
    		$data['info'] = $this->getDataInfo($this->model, $conditions, $field);
    		$this->showView($data);
    	}
    }

    /**
     * 获取菜单树
     * @param $pk_id
     */
    public function getMenuTree($pk_id){
        //当前操作的用户组信息
        $role_info =  $this->model->where('pk_id = %d',$pk_id)->field('pk_id,name,menu_auth')->find();
        $role_info['menu_auth'] = explode(',',$role_info['menu_auth']);
        $this->data['info'] = $role_info;

        //获取所有模块及其菜单
        $list = $this->getRows('SysMenu','','pk_id,pid,name');
        $menuTree = new \Common\Controller\TreeController();
        $this->data['rows'] = $menuTree->toTree($list,'pk_id','pid','son');
        $this->rtnResult(0);
    }

    /**
     * 获取操作树
     * @param $pk_id
     */
    public function getAccessTree($pk_id){
        //当前操作的用户组信息
        $role_info =  $this->model->where('pk_id = %d',$pk_id)->field('pk_id,name,menu_auth,access_auth')->find();
        $role_info['access_auth'] = explode(',',$role_info['access_auth']);

        //获取可访问菜单
        $field = 'pk_id,pid,name';
        $map['pid'] = array('neq',0);
        $map['pk_id'] = array('in',explode(',', $role_info['menu_auth']));
        $list = $this->getRows('SysMenu',$map,$field);
        $menuTree = new \Common\Controller\TreeController();
        $rows = $menuTree->toTree($list,'pk_id','pid','son');
        if (!empty($map)){
            $field = 'pk_id,name';
            foreach ($rows as $key => $value){
                //无下级菜单
                if (empty($value['son'])){
                    //获取菜单操作
                    $rows[$key]['son'] = $this->getRows('SysAccess', array('fk_menu_id'=>$value['pk_id']) ,$field);
                }else {
                    foreach ($value['son'] AS $k => $v){
                        $rows[$key]['son'][$k]['son'] = $this->getRows('SysAccess', array('fk_menu_id'=>$v['pk_id']) ,$field);
                    }
                }
            }
        }
        $this->data['info'] = $role_info;
        $this->data['rows'] = $rows;
        $this->rtnResult(0);
    }

    /**
     * 访问授权
     * Enter description here ...
     */
    public function access(){
        if(IS_AJAX){
            $menu = I('menu'); //可访问菜单
            $menu && $_POST['menu_auth'] = implode(',', $menu);
            $access = I('post.access');
            $access && $_POST['access_auth'] = implode(',', $access);
            $this->editData($this->model,'设置成功|设置失败|ajax"','index');
        }else{
            //角色列表
            $field = 'pk_id,name';
            $map['pk_id'] = array('neq',1);
            $data['role_rows'] = $this->getRows(self::modelName, $map ,$field);

            //当前操作的用户组信息
            $id = I('pk_id');
            $role_info =  $this->model->where('pk_id = %d',$id)->field('pk_id,name,menu_auth')->find();
            $data['info'] = $role_info;

            //获取所有模块及其菜单
            $field = 'pk_id,pid,name';
            $list = $this->getRows('sys_menu','',$field);
            $menuTree = new \Common\Controller\TreeController();
            $data['rows'] = $menuTree->toTree($list,'pk_id','pid','son');
            $this->showView($data);
        }
    }
}
?>